import { ElMessage } from 'element-plus'
import router from 'router'
import authService from '@/services/authService';

// 处理响应错误
export const responseErrorHandler = (error) => {
  const { response } = error
  if (response) {
    const { status, data } = response
    const msg = data.message
    switch (status) {
      case 400:
        ElMessage.error(msg || '请求错误');
        break;
      case 401:
        ElMessage.error(msg || '登录已过期，请重新登录');
        authService.loginOut();
        router.push('/login');
        break;
      case 403:
        ElMessage.error(msg || '请先登录后再访问');
        authService.loginOut();
        router.push('/login');
        break;
      case 404:
        ElMessage.error('请求资源未找到');
        break;
      case 500:
        ElMessage.error('服务器内部错误');
        break;
      default:
        ElMessage.error(`连接错误${status}`);
    }
  } else {
    ElMessage.error('网络异常，无法连接服务器');
  }
  return Promise.reject(error);
}
